package com.wish.group05.shoufang.controller;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.group05.base.entity.SfTermination;
import com.wish.group05.base.service.ISfTerminationService;
import com.wish.group05.common.core.utils.StringUtils;
import freemarker.template.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wish.group05.common.log.annotation.Log;
import com.wish.group05.common.log.enums.BusinessType;
import com.wish.group05.common.security.annotation.PreAuthorize;
import com.wish.group05.common.core.web.controller.BaseController;
import com.wish.group05.common.core.web.domain.AjaxResult;
import com.wish.group05.common.core.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author wish
 * @date 2022-05-27
 */
@RestController
@RequestMapping("/termination")
public class TSfTerminationController extends BaseController
{
    @Autowired
    private ISfTerminationService iSfTerminationService;

    @Resource
    JavaMailSender javaMailSender;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize(hasPermi = "shoufang:termination:list2")
    @GetMapping("/list")
    public TableDataInfo list(SfTermination sfTermination)
    {
        QueryWrapper<SfTermination> queryWrapper = new QueryWrapper<>();
        startPage();
        //解约协议编号
        if(!StringUtils.isEmpty(sfTermination.getSfTerminationNo())){
            queryWrapper.like("sf_termination_no",sfTermination.getSfTerminationNo());
        }
        //查询小区名称
        if(!StringUtils.isEmpty(sfTermination.getPremiseName())){
            queryWrapper.like("premise_name",sfTermination.getPremiseName());
        }
        //查询录入人
        if(!StringUtils.isEmpty(sfTermination.getInputUserName())){
            queryWrapper.like("input_user_name",sfTermination.getInputUserName());
        }
        //查询录入日期
        if(sfTermination.getInputDate()!=null){
            queryWrapper.gt("input_date",sfTermination.getInputDate());
        }
        List<SfTermination> list = iSfTerminationService.list(queryWrapper);
        return getDataTable(list);
    }

 /*   *//**
     * 导出【请填写功能名称】列表
     *//*
    @PreAuthorize(hasPermi = "system:termination:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TSfTermination tSfTermination) throws IOException
    {
        List<TSfTermination> list = tSfTerminationService.selectTSfTerminationList(tSfTermination);
        ExcelUtil<TSfTermination> util = new ExcelUtil<TSfTermination>(TSfTermination.class);
        util.exportExcel(response, list, "termination");
    }*/


     //获取详细信息
    @PreAuthorize(hasPermi = "shoufang:termination:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        SfTermination sfTermination = iSfTerminationService.getById(id);
        return AjaxResult.success(sfTermination);
    }

    //修改
    @PreAuthorize(hasPermi = "shoufang:termination:edit")
    @Log(title = "【修改】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SfTermination sfTermination)throws IOException, TemplateException
    {
        boolean b = iSfTerminationService.saveOrUpdate(sfTermination);
        // 解析 模版和 数据
        Map<String, Object> map = new HashMap<>();
        map.put("sfTermination",sfTermination);
        try {
            // 通过JavaMailSender对象创建一个MimeMessage对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 用MimeMessageHelper对象对MimeMessage对象进行各种设置
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, false, StandardCharsets.UTF_8.toString());
            Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
            configuration.setClassForTemplateLoading(this.getClass(), "/templates");
            String text = FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate("email.ftl"), map);
            helper.setFrom("1365654648@qq.com"); // 发送方邮箱地址
            helper.setTo("1365654648@qq.com"); // 接收方邮箱地址
            helper.setSubject("标题"); // 设置标题
            helper.setText(text,true);
            javaMailSender.send(mimeMessage);
        } catch (javax.mail.MessagingException e) {
            e.printStackTrace();
        }
        return toAjax(b==true?1:0);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @PreAuthorize(hasPermi = "shoufang:termination:remove")
    @Log(title = "【删除】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        boolean b = iSfTerminationService.removeByIds(Arrays.asList(ids));

        return toAjax(b==true?1:0);
    }

    @PreAuthorize(hasPermi = "shoufang:termination:add2")
    @Log(title = "【添加】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SfTermination sfTermination) throws IOException, TemplateException {
        boolean save = iSfTerminationService.save(sfTermination);
        // 解析 模版和 数据
        Map<String, Object> map = new HashMap<>();
        map.put("sfTermination",sfTermination);
        try {
            // 通过JavaMailSender对象创建一个MimeMessage对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 用MimeMessageHelper对象对MimeMessage对象进行各种设置
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, false, StandardCharsets.UTF_8.toString());
            Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
            configuration.setClassForTemplateLoading(this.getClass(), "/templates");
            String text = FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate("email.ftl"), map);
            helper.setFrom("1365654648@qq.com"); // 发送方邮箱地址
            helper.setTo("1365654648@qq.com"); // 接收方邮箱地址
            helper.setSubject("标题"); // 设置标题
            helper.setText(text,true);
            javaMailSender.send(mimeMessage);
        } catch (javax.mail.MessagingException e) {
            e.printStackTrace();
        }


        return toAjax(save==true?1:0);
    }
}
